package com.hlg.xboot.web.view;

import org.apache.commons.io.FilenameUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractXlsxView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>Description: Excel数据导出视图</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 中视数讯</p>
 * <p>Site: www.digi-zones.com</p>
 *
 * @author yangxin
 * @version 1.0
 * @date 2016/9/9
 */
@Component
public class ExportExcelView extends AbstractXlsxView {

    /**
     * 默认边框
     */
    protected short cellBorder = 1;

    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
                                      HttpServletResponse response) throws Exception {
        String fileName = model.get("fileName").toString() + ".xlsx";
        List<String> headerRows = (List<String>) model.get("header");
        List<List<String>> rows = (List<List<String>>) model.get("rows");

        // 导出的文件名
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"),"iso-8859-1"));

        // 默认列宽
        Sheet sheet = workbook.createSheet(FilenameUtils.getName(fileName));
        setSheetStyle(sheet);

        // 标题
        Row headerRow = sheet.createRow(0);
        setHeaderRowStyle(headerRow);
        CellStyle headerStyle = createHeaderCellStyle(workbook);
        for (int i = 0; i < headerRows.size(); i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellStyle(headerStyle);
            cell.setCellValue(headerRows.get(i));
        }

        // 数据
        CellStyle dataStyle = createCellStyle(workbook);
        for (int i = 0; i < rows.size(); i++) {
            Row dataRow = sheet.createRow(i + 1);
            setDataRowStyle(dataRow);
            List<String> columnValues = rows.get(i);
            for (int j = 0; j < columnValues.size(); j++) {
                String columnValue = columnValues.get(j);
                Cell cell = dataRow.createCell(j);
                cell.setCellStyle(dataStyle);
                cell.setCellValue(columnValue);
            }
        }
    }

    /**
     * 创建单元格样式
     * @param workbook  工作薄实例
     * @return
     */
    protected CellStyle createCellStyle(Workbook workbook) {
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderTop(cellBorder);
        cellStyle.setBorderRight(cellBorder);
        cellStyle.setBorderBottom(cellBorder);
        cellStyle.setBorderLeft(cellBorder);
        cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        return cellStyle;
    }

    /**
     * 表头样式
     * @param workbook  工作薄实例
     * @return
     */
    protected CellStyle createHeaderCellStyle(Workbook workbook) {
        CellStyle headerStyle = createCellStyle(workbook);
        headerStyle.setAlignment(CellStyle.ALIGN_CENTER);
        headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        XSSFFont font = (XSSFFont) workbook.createFont();
        font.setBold(true);
        font.setFontHeight(14);
        headerStyle.setFont(font);
        return headerStyle;
    }

    /**
     * 设置电子表格式样式
     * @param sheet
     */
    protected void setSheetStyle(Sheet sheet) {
        sheet.setDefaultColumnWidth((short)14);
    }

    /**
     * 设置表头样式
     * @param headerRow
     */
    protected void setHeaderRowStyle(Row headerRow) {
        headerRow.setHeightInPoints(30);
    }

    /**
     * 设置数据行样式
     * @param dataRow
     */
    private void setDataRowStyle(Row dataRow) {
        dataRow.setHeightInPoints(20);
    }

    public short getCellBorder() {
        return cellBorder;
    }

    public void setCellBorder(short cellBorder) {
        this.cellBorder = cellBorder;
    }
}
